package Department

import (
	"ali-hr/Database"
	"ali-hr/Models"
	"ali-hr/Services/Department"
	"ali-hr/Util"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/gookit/goutil/dump"
)

/**
 * Created by Goland
 * User: wkk alisleepy@hotmail.com
 * Time: 2022/3/27 - 17:25
 * Desc: <部门相关>
 */

// List 部门列表
func List(c *gin.Context) {
	dump.P("开始获取部门列表。。。")
	pageIndex, err1 := strconv.Atoi(c.Query("pageIndex"))
	pageSize, err2 := strconv.Atoi(c.Query("pageSize"))
	name := c.Query("name")
	parentId := c.Query("parent_id")
	leaderId := c.Query("leader_id")
	status := c.Query("status")
	if err1 != nil || err2 != nil {
		dump.P("参数错误， err信息如下：", err1, err2)
		Util.FailWithMessage("参数错误", c)
		return
	}
	if pageIndex == 0 || pageSize == 0 {
		Util.FailWithMessage("参数值不能为0", c)
		return
	}
	// 定义请求参数
	form := make(map[string]interface{})
	form["name"] = name
	form["parent_id"] = parentId
	form["leader_id"] = leaderId
	form["status"] = status
	if Util.Debug() {
		dump.P(pageIndex, pageSize, form)
	}
	// 获取部门列表
	departments, total := Department.List(pageIndex, pageSize, form, false)

	// 定义返回值：map
	result := make(map[string]interface{})
	result["data"] = departments
	result["total"] = total
	Util.OkWithData(result, c)
}

// Info 部门详情
func Info(c *gin.Context) {
	dump.P("部门详情。。。")
	departmentId, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		dump.P("部门详情err:", err)
		Util.FailWithMessage("参数错误", c)
	}

	var result = make(map[string]interface{})
	departmentInfo := Department.GetDepartmentInfo(departmentId)
	result["department"] = departmentInfo
	Util.OkWithData(result, c)
}

// Create 创建部门
func Create(c *gin.Context) {
	dump.P("开始添加部门。。。")
	name := c.PostForm("name")
	parentId := c.PostForm("parent_id")
	leaderId := c.PostForm("leader_id")
	status := c.PostForm("status")
	companyId := c.PostForm("company_id")
	if Util.Debug() {
		dump.P("部门name：", name)
		dump.P("部门parentId：", parentId)
		dump.P("部门leaderId：", leaderId)
		dump.P("部门status：", status)
		dump.P("部门companyId：", companyId)
	}

	form := make(map[string]interface{})
	form["name"] = name
	form["parent_id"] = parentId
	form["leader_id"] = leaderId
	form["status"] = status
	form["company_id"] = companyId
	for _, val := range form {
		if val == "" || val == nil {
			Util.FailWithMessage("值不能为空", c)
			return
		}
	}

	// 添加
	ok, msg := Department.CreateDepartment(form)
	if !ok {
		Util.FailWithMessage(msg, c)
		return
	}
	Util.OkWithMessage("添加成功", c)
}

// Update 更新部门
func Update(c *gin.Context) {
	dump.P("开始编辑部门。。。")
	id, _ := strconv.Atoi(c.PostForm("id"))
	name := c.PostForm("name")
	parentId, _ := strconv.Atoi(c.PostForm("parent_id"))
	leaderId, _ := strconv.Atoi(c.PostForm("leader_id"))
	status := c.PostForm("status")
	companyId, _ := strconv.Atoi(c.PostForm("company_id"))
	if id == 0 {
		Util.FailWithMessage("ID参数有误", c)
		return
	}
	if Util.Debug() {
		dump.P("部门ID：", id)
		dump.P("部门name：", name)
		dump.P("部门parentId：", parentId)
		dump.P("部门leaderId：", leaderId)
		dump.P("部门status：", status)
		dump.P("部门companyId：", companyId)
	}

	var form = make(map[string]interface{})
	form["name"] = name
	form["parent_id"] = parentId
	form["leader_id"] = leaderId
	form["status"] = status
	form["company_id"] = companyId
	for _, val := range form {
		if val == "" || val == nil {
			Util.FailWithMessage("值不能为空", c)
			return
		}
	}

	// 修改
	ok, msg := Department.UpdateDepartment(uint(id), form)
	if !ok {
		Util.FailWithMessage(msg, c)
		return
	}
	Util.OkWithMessage("更新成功", c)
}

// Delete 删除部门, delete操作
func Delete(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	if id == 0 {
		Util.FailWithMessage("ID参数不能为空", c)
		return
	}
	ok, msg := Department.DeleteDepartment(uint(id))
	if !ok {
		Util.FailWithMessage(msg, c)
		return
	}
	Util.OkWithMessage("删除成功", c)
}

// ChangeStatus 修改部门状态：开启/生效/封存操作
func ChangeStatus(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("id"))
	actionType := c.PostForm("type")
	if id == 0 {
		Util.FailWithMessage("ID参数不能为空", c)
		return
	}
	if len(actionType) == 0 {
		Util.FailWithMessage("非法参数", c)
		return
	}

	var ok bool
	var msg string
	switch actionType {
	case "封存":
		ok, msg = Department.CloseDepartment(uint(id))
	case "生效":
	case "启用":
		ok, msg = Department.EffectDepartment(uint(id))
	}
	if !ok {
		Util.FailWithMessage(msg, c)
		return
	}
	Util.OkWithMessage("操作成功", c)
}

// All 所有部门，前端组件使用
func All(c *gin.Context) {
	departments := Department.AllDepartments()
	Util.OkWithData(departments, c)
}

// Children 获取部门的子部门
func Children(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	children := Department.GetDepartmentChildren(uint(id), false)
	if children == nil {
		Util.FailWithMessage("获取失败", c)
		return
	}

	Util.OkWithData(children, c)
}

// AllChildren 获取部门的所有子部门，包括孙子部门
func AllChildren(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	allChildren := Department.GetAllDepartmentChildren(uint(id), nil)
	if allChildren == nil {
		Util.FailWithMessage("获取失败", c)
		return
	}

	Util.OkWithData(allChildren, c)
}

// Export 导出部门信息
func Export(c *gin.Context) {
	dump.P("开始导出部门。。。")
	name := c.Query("name")
	parentId, _ := strconv.Atoi(c.Query("parent_id"))
	leaderId, _ := strconv.Atoi(c.Query("leader_id"))
	status := c.Query("status")

	// 定义请求参数
	form := make(map[string]interface{})
	form["name"] = name
	form["parent_id"] = parentId
	form["leader_id"] = leaderId
	form["status"] = status

	/** 方式一：直接导出Excel start **/
	//fileName, filePath, err := Jobs.ExportDepartment(form)
	//if err != nil {
	//	dump.P("导出部门失败，err信息：", err)
	//	Util.FailWithMessage("导出失败", c)
	//	return
	//}
	//result := make(map[string]string)
	//result["file_path"] = filePath
	//result["file_name"] = fileName
	//Util.OkWithData(result, c)
	/** 方式一  end **/

	/** 方式二：导出文件，写到队列中异步执行 start **/
	ok, msg := Department.ExportDepartment(form)
	if !ok {
		Util.FailWithMessage(msg, c)
		return
	}
	result := make(map[string]string)
	result["file_name"] = "测试部门导出"
	Util.OkWithData(result, c)
	/** 方式二 end **/
}

// Tree 部门树，一次全部取出
func Tree(c *gin.Context) {
	// 查询input
	name := c.Query("name")
	parentId, _ := strconv.Atoi(c.Query("parent_id"))
	leaderId := c.Query("leader_id")
	status := c.Query("status")
	// 定义请求参数
	form := make(map[string]interface{})
	form["name"] = name
	form["parent_id"] = parentId
	form["leader_id"] = leaderId
	form["status"] = status

	var Db = Database.SqlDB.Where("status != ?", Models.DepartmentClose)
	// 查询
	for key, value := range form {
		if value == 0 || value == "" || value == nil {
			continue
		}
		if key == "parent_id" {
			Db = Db.Where("parent_id = ?", value)
		} else if key == "leader_id" {
			Db = Db.Where("leader_id = ?", value)
		} else if key == "name" {
			Db = Db.Where("name = ?", value)
		} else if key == "status" {
			Db = Db.Where("status = ?", value)
		}
	}
	var departmentList []Models.Department
	err := Db.Find(&departmentList).Error
	if err != nil {
		Util.FailWithMessage("暂无数据", c)
		return
	}
	// 无限极分类，获取部门树
	var pId int
	if parentId > 0 {
		pId = parentId
	}
	departmentTree := Department.GetDepartmentTree(departmentList, pId)
	if departmentTree == nil {
		Util.FailWithMessage("暂无数据", c)
		return
	}
	//var result map[string]Models.DepartmentTree
	tree := Models.DepartmentTree{
		Id:       0,
		Name:     "阿狸科技有限公司",
		Children: departmentTree,
		ParentId: 0,
		Expand:   true,
	}
	Util.OkWithData(tree, c)
}
